package com.shanyu.wechat.util;

import com.shanyu.wechat.entity.JianDaoYunEntity;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;

@UtilityClass
public class JianDaoYunUtil {

    @SneakyThrows
    public void send(String name, String age) {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\r\n    \"data\": {\r\n        \"_widget_1634386434470\": {\r\n            \"value\": \"" + name + "\"\r\n        },\r\n        \"_widget_1634388572573\": {\r\n            \"value\": " + age + "\r\n        }\r\n    },\r\n    \"is_start_trigger\": true\r\n}");
        Request request = new Request.Builder()
                .url("https://api.jiandaoyun.com/api/v3/app/616ac2218b1bfe0008c4d9c5/entry/616ac24c9307f60007d225a7/data_create")
                .method("POST", body)
                .addHeader("Authorization", "Bearer wjr97ktphv7bgHI2O4xq1x7hk14GQ8PG")
                .addHeader("User-Agent", "apifox/1.0.0 (https://www.apifox.cn)")
                .addHeader("Content-Type", "application/json")
                .build();
        Response response = client.newCall(request).execute();
        System.out.println(response.code());
    }

    public static final String JIANDAOYUN_BODY = "{\n" +
            "  \"data\": {\n" +
            "    \"_widget_1635152653882\": {\n" +
            "      \"value\": \"${message}\"\n" +
            "    },\n" +
            "    \"_widget_1635152654033\": {\n" +
            "      \"value\": \"${receiveTime}\"\n" +
            "    },\n" +
            "    \"_widget_1635152654468\": {\n" +
            "      \"value\": \"${fromUser}\"\n" +
            "    },\n" +
            "    \"_widget_1635152654566\": {\n" +
            "      \"value\": \"${toUser}\"\n" +
            "    },\n" +
            "    \"_widget_1635152654601\": {\n" +
            "      \"value\": \"${area}\"\n" +
            "    },\n" +
            "    \"_widget_1635152654660\": {\n" +
            "      \"value\": \"${man}\"\n" +
            "    },\n" +
            "    \"_widget_1635152654680\": {\n" +
            "      \"value\": \"${phone}\"\n" +
            "    },\n" +
            "    \"_widget_1635152655972\": {\n" +
            "      \"value\": \"\"\n" +
            "    },\n" +
            "    \"_widget_1635152655985\": {\n" +
            "      \"value\": \"\"\n" +
            "    },\n" +
            "    \"_widget_1635152656036\": {\n" +
            "      \"value\": \"\"\n" +
            "    }\n" +
            "  },\n" +
            "  \"is_start_workflow\": true,\n" +
            "  \"is_start_trigger\": true\n" +
            "}";

    @SneakyThrows
    public void sendFlow(JianDaoYunEntity jianDaoYunEntity) {
        String param = JIANDAOYUN_BODY
                .replace("${message}", jianDaoYunEntity.getMessage())
                .replace("${message}", jianDaoYunEntity.getMessage())
                .replace("${receiveTime}", jianDaoYunEntity.getReceiveTime())
                .replace("${fromUser}", jianDaoYunEntity.getFromUser())
                .replace("${toUser}", jianDaoYunEntity.getToUser())
                .replace("${area}", jianDaoYunEntity.getArea())
                .replace("${man}", jianDaoYunEntity.getMan())
                .replace("${phone}", jianDaoYunEntity.getPhone());


        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, param);
        Request request = new Request.Builder()
                .url("https://api.jiandaoyun.com/api/v3/app/61767b8dabcc18000750ebf3/entry/6176575a4b5f740007a6ab79/data_create")
                .method("POST", body)
                .addHeader("Authorization", "Bearer wjr97ktphv7bgHI2O4xq1x7hk14GQ8PG")
                .addHeader("User-Agent", "apifox/1.0.0 (https://www.apifox.cn)")
                .addHeader("Content-Type", "application/json")
                .build();
        Response response = client.newCall(request).execute();
        System.out.println("**********************************************************发送简道云 code is");
        System.out.println(response.code());
        System.out.println(response.body().string());
    }

}
